<template>
  <div class="order-container">
    <!-- tab组件 -->
    <XtxTabs v-model="active" @tab-click="changeTab">
      <XtxTabsPanel
        v-for="(item, i) in tabPanel"
        :key="i"
        :label="item.label"
        :name="item.label"
      ></XtxTabsPanel>
    </XtxTabs>
    <!-- 订单组件 -->
    <div class="order-list">
      <div v-if="loading" class="loading"></div>
      <div class="none" v-if="!loading && orderList.length === 0">暂无数据</div>
      <OrderItem
        v-if="!loading"
        v-for="item in orderList"
        :key="item.id"
        :order="item"
        :state="tabPanel"
        @on-cancel="handlerCancel"
        @on-delete="handlerDelete"
        @on-confirmOrder="handlerConfirm"
        @on-logistics="lookLogistics"
      >
      </OrderItem>
      <!-- 分页 -->
      <XtxPagination
        v-if="total > 0 && !loading"
        :pageSize="requestParams.pageSize"
        :currentPage="requestParams.page"
        :total="total"
        @current-page="changePage"
      >
      </XtxPagination>
    </div>
    <!-- 取消订单对话框 -->
    <OrderCancel ref="cancelTager"></OrderCancel>
    <!-- 查看物流对话框 -->
    <OrderLogistics ref="lookLogisticsTarget"></OrderLogistics>
  </div>
</template>

<script>
import { reactive, ref, watch } from 'vue'
import { findOrderList, delteOrder, confirmOrder } from '@/api/order'
import OrderItem from './components/order-item.vue'
import OrderCancel from './components/order-cancel.vue'
import OrderLogistics from './components/order-logistics.vue'
import Confirm from '@/components/library/confirm'
import Message from '@/components/library/Message'
export default {
  name: 'order',
  components: {
    OrderItem,
    OrderCancel,
    OrderLogistics
  },
  setup() {
    const active = ref('全部订单')
    const tabPanel = [
      {
        label: '全部订单'
      },
      {
        label: '待付款'
      },
      {
        label: '待发货'
      },
      {
        label: '待收货'
      },
      {
        label: '待评价'
      },
      {
        label: '已完成'
      },
      {
        label: '已取消'
      }
    ]
    const changeTab = tab => {
      console.log(tab)
      requestParams.page = 1
      requestParams.orderState = tab.index
    }
    // 查询订单参数
    const requestParams = reactive({
      page: 1,
      pageSize: 5,
      orderState: 0
    })
    // 订单列表
    const orderList = ref([])
    const loading = ref(false)
    // 总条数
    const total = ref(0)
    const getOrderList = () => {
      // 查询订单
      loading.value = true
      findOrderList(requestParams).then(data => {
        orderList.value = data.result.items
        total.value = data.result.counts
        loading.value = false
        console.log(orderList.value)
      })
    }
    watch(
      requestParams,
      () => {
        getOrderList()
      },
      { immediate: true }
    )

    // 分页
    const changePage = page => {
      requestParams.page = page
      // window.document.documentElement.scrollTop = 0
      window.scrollTo({
        top: 0,
        behavior: 'smooth'
      })
    }
    // 删除订单
    const handlerDelete = order => {
      const arr = [order.id]
      console.log(order)
      Confirm({ title: '删除订单', text: `是否删除订单编号为${order.id}的订单` })
        .then(() => {
          delteOrder(arr).then(() => {
            Message({ type: 'success', text: '删除订单成功' })
            getOrderList()
            order.orderState = 4
          })
        })
        .catch(() => {})
    }
    return {
      active,
      changeTab,
      tabPanel,
      orderList,
      loading,
      changePage,
      total,
      requestParams,
      ...useCancel(),
      handlerDelete,
      getOrderList,
      ...useConfirm(),
      ...useLogistics()
    }
  }
}
// 取消订单逻辑
const useCancel = () => {
  // 组件实例
  const cancelTager = ref(null)
  const handlerCancel = order => {
    console.log(order)
    console.log(cancelTager)
    cancelTager.value.open(order)
  }
  return { handlerCancel, cancelTager }
}
// 确认收货逻辑
const useConfirm = () => {
  const handlerConfirm = order => {
    Confirm({ title: '确认收货', text: '确认收货后，将会把货款打给商家' })
      .then(() => {
        confirmOrder(order.id).then(() => {
          Message({ type: 'success', text: '确认收货成功' })
          getOrderList()
        })
      })
      .catch(() => {})
  }
  return { handlerConfirm }
}
// 查看物流
const useLogistics = () => {
  const lookLogisticsTarget = ref(null)
  const lookLogistics = order => {
    lookLogisticsTarget.value.open(order)
  }
  return { lookLogistics, lookLogisticsTarget }
}
</script>

<style lang="less" scoped>
.order-list {
  padding: 20px;
  position: relative;
  min-height: 400px;
  background: #fff;
  padding: 20px;
}

.loading {
  height: 100%;
  width: 100%;
  position: absolute;
  left: 0;
  top: 0;
  background: rgba(255, 255, 255, 0.9) url(../../../assets/images/load.gif) no-repeat center;
  z-index: 9999;
}

.none {
  height: 400px;
  text-align: center;
  line-height: 400px;
  color: #999;
}
</style>
